package com.saint.lt.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.saint.lt.domain.Comment;
import com.saint.lt.domain.Issue;

@Repository
public class CommentDAOImpl implements CommentDAO {

	@Autowired
	private SessionFactory sessionFactory;

	@Override
	public void add(Comment comment) {
		sessionFactory.getCurrentSession().save(comment);
	}

	@Override
	public void remove(Comment comment) {
		if (null != comment) {
			sessionFactory.getCurrentSession().delete(comment);
		}

	}

	@Override
	public void remove(int id) {
		Comment comment = (Comment) sessionFactory.getCurrentSession().load(Comment.class,
				id);
		remove(comment);
	}

	@Override
	public List<Comment> list() {
		List<Comment> comments = sessionFactory.getCurrentSession().createCriteria(Comment.class).list();
		return comments;
	}

	@Override
	public Comment get(int id) {
		Criteria cr = sessionFactory.getCurrentSession().createCriteria(Comment.class);
		cr.add(Restrictions.eq("id", id));
		Comment result = (Comment) cr.uniqueResult();
		return result;
	}

	@Override
	public List<Comment> list(Issue issue) {
		Criteria cr = sessionFactory.getCurrentSession().createCriteria(Comment.class);
		cr.add(Restrictions.eq("issue", issue));
		List<Comment> comments = cr.list();
		return comments;
	}

}
